﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCTippSpielManagement.Utilities;
using System.Collections;
using System.Configuration;

namespace MVCTippSpielManagement.Management
{
	public class RightsManager
	{
		public UserMenu _AllMenuEntries;


		public RightsManager()
		{
			
		}

		private void LoadMenuXML()
		{
			//Deserialisiere Menu
			try
			{
				string XMLPath;
				AppSettingsReader asr = new AppSettingsReader();
				XMLPath = (string)asr.GetValue("XMLUserMenu", typeof(string));
				_AllMenuEntries = new UserMenu();
				Util.Deserialize<UserMenu>(XMLPath, out _AllMenuEntries);
			}
			catch (Exception e)
			{

			}
		}
		public UserMenu GetUserMenu(User usr)
		{

			UserMenu _UserMenu = new UserMenu();

			// Read Menü aus XML-Datei
			if (_AllMenuEntries == null) { LoadMenuXML();}
			

			//Über Menueinträge iterieren und dementsprechend
		    //Menueinträge ein/ausblenden
			for (int i = 0; i < _AllMenuEntries.Items.Count(); i++)
			{

				MyMenuItem _UserMenuItem = (MyMenuItem)_AllMenuEntries.Items[i];

				// Check UserGroups
				ArrayList _XMLGroups = _UserMenuItem.UserGroups;
				if (CheckUserGroups(usr, _XMLGroups) == true)
				{

					MyMenuItem _NewMenuItem = new MyMenuItem();

					_NewMenuItem.Name = _UserMenuItem.Name;
					_NewMenuItem.Text = _UserMenuItem.Text;
					_NewMenuItem.ShortCut = _UserMenuItem.ShortCut;
					_NewMenuItem.Enabled = _UserMenuItem.Enabled;
					_NewMenuItem.UserGroups = _UserMenuItem.UserGroups;

					// Fill SubItems
					_NewMenuItem.MenuItems = GetMeueItems(usr, _UserMenuItem.MenuItems);

					// Add Menü
					_UserMenu.Add(_NewMenuItem);

				}

			}


			return _UserMenu;
		
		}

		private bool CheckUserGroups(User usr, ArrayList _MenuGroups)
		{

			ArrayList _CheckGroups;
			
			bool checkOK;
			Dictionary<int, string> _UserGroups;

			_UserGroups = (Dictionary<int, string>)usr.UserGroups;
			checkOK = false;

			_CheckGroups = new ArrayList();
			foreach (int item in _UserGroups.Keys)
			{
				switch (item)
				{
					case 2:
						_CheckGroups.Add("ADMIN");
						break;
					case 3:
						_CheckGroups.Add("GUEST");
						break;
					case 4:
						_CheckGroups.Add("PLAYER");
						break;
				}
				
			}


			foreach (string _checkGroup in _CheckGroups) // Groups from User
			{
				foreach (System.Xml.XmlNode[] item in _MenuGroups)  // Groups from XML-Menu
				{

					for (int i = 0; i < item.Count(); i++)
					{
						if (_checkGroup == System.Convert.ToString(item[i].InnerText))
						{
							checkOK = true;
						}
					}
				}

			}

			return checkOK;
		}

		private List<MyMenuItem> GetMeueItems(User usr, List<MyMenuItem> _Items)
		{

			List<MyMenuItem> _NewItems = new List<MyMenuItem>();


			foreach (MyMenuItem item in _Items)
			{


				ArrayList _XMLGroups = item.UserGroups;
				if (CheckUserGroups(usr, _XMLGroups) == true)
				{

					MyMenuItem _NewMenuItem = new MyMenuItem();

					_NewMenuItem.Name = item.Name;
					_NewMenuItem.Text = item.Text;
					_NewMenuItem.ShortCut = item.ShortCut;
					_NewMenuItem.Enabled = item.Enabled;
					_NewMenuItem.UserGroups = item.UserGroups;

					_NewItems.Add(_NewMenuItem);
				}

			}

			return _NewItems;
		}


	}
}
